package com.trainoo.mina.codec;

import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolEncoder;

import java.nio.charset.Charset;

/**
 * @Auther: zt
 * @Date: 2020/4/14 16:19
 * @Description:
 */
public class MessageCodecFactory implements ProtocolCodecFactory {

    private final ProtocolEncoder encoder;
    private final ProtocolDecoder decoder;

    public MessageCodecFactory() {
        this.encoder = new MessageEncoder();
        this.decoder = new MessageDecoder();
    }

    public MessageCodecFactory(Charset charset) {
        this.encoder = new MessageEncoder(charset);
        this.decoder = new MessageDecoder(charset);
    }

    @Override
    public ProtocolEncoder getEncoder(IoSession session) throws Exception {
        return encoder;
    }

    @Override
    public ProtocolDecoder getDecoder(IoSession session) throws Exception {
        return decoder;
    }
}
